package com.example.cushion.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;


import com.example.cushion.R;
import com.example.cushion.bean.MyMenuItem;

import org.itzheng.and.baseutils.common.ArrayUtils;
import org.itzheng.and.baseutils.common.StrUtils;
import org.itzheng.and.baseutils.ui.UIUtils;
import org.itzheng.and.recyclerview.adapter.BaseRecyclerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2017/8/21.
 */
public class MenuItemAdapter extends BaseRecyclerAdapter<MenuItemAdapter.ViewHolder> {
    private List<MyMenuItem> mItems;
    /**
     * 设置item是否可以点击，如果外面设置点击事件，则item变为可点击
     */
    private boolean mItemClickable = false;

    public void setItemClickable(boolean itemClickable) {
        mItemClickable = itemClickable;
    }

    public MenuItemAdapter(List<MyMenuItem> menuItems) {
        mItems = menuItems;
    }

    /**
     * 默认颜色。没有设置颜色或者还原成系统默认的颜色
     */
    public static final int DEF_COLOR = -1;
    private int mSubTitleColor = DEF_COLOR;

    public MenuItemAdapter setSubTitleColor(int color) {
        mSubTitleColor = color;
        return this;
    }

    private int mTitleColor = DEF_COLOR;

    public MenuItemAdapter setTitleColor(int color) {
        mTitleColor = color;
        return this;
    }

    @Override
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        super.setOnItemClickListener(onItemClickListener);
        //如果点击事件为空，item依然为不可点击
        mItemClickable = onItemClickListener != null;
    }

    @Override
    public int getItemCount() {
        return ArrayUtils.size(mItems);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            default:
                return new ViewHolder(onCreateView(parent, viewType));
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MyMenuItem menuItem = mItems.get(position);
        //设置是否可点击
        holder.itemView.setEnabled(menuItem.onClickListener != null || mItemClickable);
        if (mSubTitleColor != DEF_COLOR) {
            holder.tvSubTitle.setTextColor(mSubTitleColor);
        }
        if (mTitleColor != DEF_COLOR) {
            holder.tvTitle.setTextColor(mTitleColor);
        }
        switch (menuItem.type) {
            case MyMenuItem.TYPE_COMMON_TEXT:
                holder.tvTitle.setText(StrUtils.toString(menuItem.name));
                holder.tvTitle.setVisibility(View.VISIBLE);
                holder.tvSubTitle.setText(StrUtils.toString(menuItem.subName));
                holder.tvSubTitle.setVisibility(View.VISIBLE);
                holder.ivIcon.setVisibility(View.GONE);
                holder.ivArrowRight.setVisibility(View.GONE);
                holder.btnToggle.setVisibility(View.GONE);
                break;
            case MyMenuItem.TYPE_ARROW_RIGHT:
                holder.tvTitle.setText(StrUtils.toString(menuItem.name));
                holder.tvTitle.setVisibility(View.VISIBLE);
                holder.tvSubTitle.setText(StrUtils.toString(menuItem.subName));
                holder.tvSubTitle.setVisibility(View.VISIBLE);
                holder.ivIcon.setVisibility(View.GONE);
                holder.ivArrowRight.setVisibility(View.VISIBLE);
                holder.btnToggle.setVisibility(View.GONE);
                break;
            case MyMenuItem.TYPE_TOGGLE_SWITCH:

                holder.tvTitle.setText(StrUtils.toString(menuItem.name));
                holder.tvTitle.setVisibility(View.VISIBLE);
                holder.tvSubTitle.setText(StrUtils.toString(menuItem.subName));
                holder.tvSubTitle.setVisibility(View.GONE);
                holder.ivIcon.setVisibility(View.GONE);
                holder.ivArrowRight.setVisibility(View.GONE);
                holder.btnToggle.setVisibility(View.VISIBLE);
                holder.btnToggle.setChecked(menuItem.status);
                holder.btnToggle.setOnCheckedChangeListener(menuItem.onCheckedChangeListener);
                break;
            case MyMenuItem.TYPE_RADIO_BUTTON://单选按钮
                holder.tvTitle.setText(StrUtils.toString(menuItem.name));
                holder.tvTitle.setVisibility(View.VISIBLE);
                holder.tvSubTitle.setText(StrUtils.toString(menuItem.subName));
                holder.tvSubTitle.setVisibility(View.GONE);
                holder.ivIcon.setVisibility(View.GONE);
                holder.ivArrowRight.setVisibility(View.GONE);
                holder.btnToggle.setVisibility(View.GONE);
                holder.btnToggle.setChecked(menuItem.status);
                holder.ivRightChecked.setVisibility(menuItem.status ? View.VISIBLE : View.GONE);
                holder.itemView.setOnClickListener(menuItem.onClickListener);
                break;
            case MyMenuItem.TYPE_ONLY_TITLE_AND_SUB_TITLE:
                holder.tvTitle.setText(StrUtils.toString(menuItem.name));
                holder.tvTitle.setVisibility(View.VISIBLE);
                holder.tvSubTitle.setText(StrUtils.toString(menuItem.subName));
                holder.tvSubTitle.setVisibility(View.VISIBLE);
                holder.ivIcon.setVisibility(View.GONE);
                holder.ivArrowRight.setVisibility(View.INVISIBLE);
                holder.btnToggle.setVisibility(View.GONE);
                break;
        }
        holder.itemView.setOnClickListener(menuItem.onClickListener);
    }

    @Override
    public View onCreateView(ViewGroup parent, int viewType) {
        return UIUtils.inflate(R.layout.adapter_item_common_menu);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivIcon)
        ImageView ivIcon;
        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.tvSubTitle)
        TextView tvSubTitle;
        @BindView(R.id.ivArrowRight)
        ImageView ivArrowRight;
        @BindView(R.id.ivRightChecked)
        ImageView ivRightChecked;
        @BindView(R.id.btnToggle)
        ToggleButton btnToggle;

        public ViewHolder(View itemView) {
            super(itemView);
            bindButterKnife();
        }

        private void bindButterKnife() {
            ButterKnife.bind(this, itemView);
        }
    }
}
